﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.LogDomain;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Domain.ProcessModuleDomain;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{

    /// <summary>
    /// 线体交互报错自动重置交互
    /// </summary>
    public class ResetCheckDataService : IPlanarGeneralPlcActionProcessService
    {
        private readonly IRedisHelper _IRedisHelper;
        private readonly ILogUtil _LogUtil;
        private readonly IPlcConnectionPool _PlcConnectionPool;
        private readonly IPlcWriteDbInfoRepository _PlcWriteDbInfoRepository;
        private readonly IRunLogInfoService _IRunLogInfoService;

        public ResetCheckDataService(IRedisHelper iRedisHelper, ILogUtil logUtil
            , IPlcConnectionPool plcConnectionPool
            , IPlcWriteDbInfoRepository plcWriteDbInfoRepository
             , IRunLogInfoService runLogInfoService)
        {
            _IRedisHelper = iRedisHelper;
            _LogUtil = logUtil;
            _PlcConnectionPool = plcConnectionPool;
            _PlcWriteDbInfoRepository = plcWriteDbInfoRepository;
            _IRunLogInfoService = runLogInfoService;
        }

        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, BytesObjectBase wcsActionMessage, EquipmentDbObject planarEquipmentDbObject/*, BytesObjectBase plcStateMassge*/)
        {
            /*
             * 处理逻辑
             * 组织plc报文
             */

            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBO;//Plc的动作报文
            var wcsMessage = wcsActionMessage as PlanarGeneralWcsActionBO;//Wcs的动作报文

            //获取设备缓存标识
            var eqValue = _IRedisHelper.GetStringKey<string>($"PLC-{plcMessage.EquipmentCode}");

            if (eqValue == null)
            {
                RunLogInfo runLogInfo = new RunLogInfo();
                runLogInfo.ContainerBarcode = wcsMessage.PalletBarcode;
                runLogInfo.EquipmentCode = wcsMessage.EquipmentCode;
                runLogInfo.RunType = "普通线体交互复位检测";

                runLogInfo.RunLog = "普通线体交互复位检测任务开始";
                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);

                //组织plc报文，写入plc(plc什么握手则回什么握手)
                var objectType = typeof(PlanarGeneralWriteWcsAction).Name;
                var writeConfig = await _PlcWriteDbInfoRepository.GetFirstAsync(it => it.EquipmentCode == plcMessage.EquipmentCode && it.ObjectType == objectType);
                if (writeConfig == null)
                {
                    //runLogInfo.RunLog = "获取PLC写入配置失败";
                    //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    _LogUtil.Error($"普通线体交互复位检测：设备【{plcMessage.EquipmentCode}】获取写DB配置失败，请检测配置！");
                    return;
                }

                //plc报文
               PlanarGeneralWriteWcsAction writeBo = new PlanarGeneralWriteWcsAction(
                    writeConfig.PlcConnectionId,
                    writeConfig.ID,
                    writeConfig.DbCode,
                    writeConfig.DbStart,
                    objectType,
                    plcMessage.EquipmentCode.ToInt32(),
                    0,//plcMessage.ActionType
                    0, //plcMessage.ToEquipmentCode,
                    0, //plcMessage.TaskCode,
                    "", //plcMessage.PalletBarcode,
                    0,
                    0//plcMessage.Alrm
                    );

                var plcConn = _PlcConnectionPool.S7GetWritePlcConnection(writeBo.PlcConnectionID);
                if (plcConn == null)
                {
                    //runLogInfo.RunLog =$"获取连接({writeBo.PlcConnectionID})失败";
                    //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    _LogUtil.Error($"普通线体交互复位检测：设备【{plcMessage.EquipmentCode}】获取连接({writeBo.PlcConnectionID})失败");
                    return;
                }
                var writeResult = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
                if (writeResult == false)
                {
                    //runLogInfo.RunLog = $"普通线体交互复位检测写入报文失败";
                    //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                    _LogUtil.Error($"普通线体交互复位检测：设备【{plcMessage.EquipmentCode}】普通线体交互复位检测写入报文失败");
                    return;
                }

                runLogInfo.RunLog = $"交互复位：【{plcMessage.EquipmentCode}】普通线体交互复位检测成功";
                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                _LogUtil.Info(runLogInfo.RunLog);
            }

        }
    }
}
